home *** CD-ROM | disk | FTP | other *** search
- Path: po.CWRU.Edu!mab22
- From: mab22@po.CWRU.Edu (Michael A. Balfour)
- Newsgroups: comp.lang.c
- Subject: Re: Variant Records in C ... Is there a way ?
- Date: 28 Mar 1996 01:51:49 GMT
- Organization: Case Western Reserve University, Cleveland, OH (USA)
- Message-ID: <4jcrbm$g5p@madeline.INS.CWRU.Edu>
- References: <Pine.OSF.3.91.960319170252.9783B-100000@alfa.ist.utl.pt> <315006F8.639@cmt.lpr.mail.carel.fi> <4iumf8$604@madeline.INS.CWRU.Edu> <danpop.827534657@rscernix>
- Reply-To: mab22@po.CWRU.Edu (Michael A. Balfour)
- NNTP-Posting-Host: kanga.ins.cwru.edu
-
-
- In a previous article, danpop@mail.cern.ch (Dan Pop) says:
-
- >In <4iumf8$604@madeline.INS.CWRU.Edu> mab22@po.CWRU.Edu (Michael A. Balfour) writes:
- >
- >>*However*, if you feel like it, you can still do it yourself with some
- >>clever mallocs and overlaying structures. Not exactly elegant, but you
- >>can make it pretty memory-efficient.
- >
- >Not exactly blessed by the C standard, either :-)
- >
-
- Maybe not blessed, but it's certainly not condemned. That's the beauty
- of pointers and typecasting (which my COBOL friends envy). But I
- suppose you've never had to do anything like this:
-
- int temp=1;
- char *temp2=(char *)&temp;
- #define ByteOrder() ((temp2[0]==0x01) ? LITTLE_ENDIAN : BIG_ENDIAN)
-
- Or this:
-
- char buffer[BUF_SIZE];
- int bufEndPtr;
-
- ...
-
- void AddIntToBuffer(int num)
- {
- short i;
- for (i=0;i<sizeof(int);i++) buffer[bufEndPtr++]=((char *)(&num))[i];
- }
-
- void AddStringToBuffer(char *s,int len)
- {
- short i;
- for (i=0;i<len;i++) buffer[bufEndPtr++]=s[i];
- }
-
- <Disclaimer: Any mistakes in the above code are because I learned C
- from a Schildt book :) >
-
- But there are plenty of cases where you might need to go walking through
- a patch of memory that contains data all packed together. So like I
- said originally, it might not be the most elegant solution, but it can
- certainly be made functional. And you can write it in ANSI C.
-
- Mike Balfour
- --
- ----------------------------------+--------------------------------
- Mike Balfour, Partner | BS/MS Graduate - ECMP
- Overload Engineering | Case Western Reserve University
- "New Ideas for a Brighter Future" | Cleveland, OH
-